/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>         /* For uint8_t definition */
#include <stdbool.h>        /* For true/false definition */

#include "user.h"

/******************************************************************************/
/* User Functions                                                             */
/******************************************************************************/

/* <Initialize variables in user.h and insert code for user algorithms.> */

void InitApp(void)
{
    /* TODO Initialize User Ports/Peripherals/Project here */
    TRISA0=1;//Idug
    TRISA1=1;//Temp
    TRISA2=1;//Ireg
    TRISA3=1;//Udug
    TRISA4=0;//led1
    TRISA5=1;
    TRISA6=0;//pwm
    TRISA7=0;//pwm
    TRISB0=0;//led2
    TRISB1=0;//led2
    TRISB2=0;//led3
    TRISB3=0;//led4
    TRISB4=0;//rel1
    TRISB5=0;//rel2
    TRISB6=0;
    TRISB7=0;

    /* Setup analog functionality and port direction */

    /* Initialize peripherals */

    /* Enable interrupts */
}

void pwrWork(int piriod, int chastota, int Deadtime, int pred){
    T2CONbits.TMR2ON=0;
    TRISA6=1;
    TRISA7=1;
    CCP2CONbits.P2M=2;//????????
    CCP2CONbits.CCP2M=0b00001100;//
    CCP2CONbits.DC2B=0;
    CCPTMRS=0;//pwm tmr2
    CCP2ASbits.CCP2AS=1;//stop pwm ?? ?1
    CCP2ASbits.PSS2AC=0;//
    CCP2ASbits.PSS2BD=0;//
    PWM2CONbits.P2RSEN=0;// ?????????? ??????????
    PWM2CONbits.P2DC=Deadtime;//Deadtime
    CCPR2L=piriod;
    PR2=chastota;
    T2CONbits.T2CKPS=pred;
    T2CONbits.TMR2ON=1;
    TRISA6=0;
    TRISA7=0;

}

void InitADC(){
    FVRCON=0b10001111;
    ADCON0bits.ADON=1;//???????? ???=0b00001101;
    ADCON1bits.ADFM=0;//???????????? ?? ?????? 0 ??? ?? ??????? 1
    ADCON1bits.ADCS=0b110;//????????????
    ADCON1bits.ADNREF=0; //????? ?????????? ? ???????
    ADCON1bits.ADPREF=0b11;//???????? ? ????????? ????? 0b00100011;
}
void startADC(unsigned char channel){
    ADCON0bits.CHS=channel;//????? ?????
    ADCON0bits.GO=1;//????? ??????????????
}
void InitPWM(){
    CCP2CON=0b10001100;
    CCPTMRS=0b00000000;//tmr2
    CCP2AS=0b00010000;//c1

}

void Startsystem(){
}

void SetPWMTosc(int a){



}